// SearchImage.cpp : implementation file
//

#include "stdafx.h"
#include "ImageLab.h"
#include "SearchImage.h"
#include "AnnotationTool.h"


// CSearchImage dialog

IMPLEMENT_DYNAMIC(CSearchImage, CDialog)

CSearchImage::CSearchImage(CWnd* pParent /*=NULL*/)
	: CDialog(CSearchImage::IDD, pParent)
{
	dialogparent = pParent;
	dialogid = IDD;
	::AfxInitRichEdit2();
}

CSearchImage::~CSearchImage()
{
}

void CSearchImage::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BOOL CSearchImage::Create()
{
	return CDialog::Create(dialogid, dialogparent);
}

BEGIN_MESSAGE_MAP(CSearchImage, CDialog)
	ON_BN_CLICKED(IDC_KEYWORDSEARCH_IMAGEQUERY, &CSearchImage::OnBnClickedKeywordsearchImagequery)
	ON_LBN_SELCHANGE(IDC_KEYWORDSEARCH_RESULTS, &CSearchImage::OnLbnSelchangeKeywordsearchResults)
	ON_BN_CLICKED(IDC_KEYWORDSEARCH_SAVETOFOLDER, &CSearchImage::OnBnClickedKeywordsearchSavetofolder)
END_MESSAGE_MAP()


// CSearchImage message handlers

void CSearchImage::OnBnClickedKeywordsearchImagequery()
{
	((CAnnotationTool*)dialogparent)->SetSearch();
	if(querylist.size()==0)
		return;

	//Get the searching text
	CEdit* searchedit = (CEdit*)CSearchImage::GetDlgItem(IDC_KEYWORDSEARCH_QUERY);
	CString searchtextbig;
	searchedit->GetWindowTextW(searchtextbig);
	CT2CA asciistring(searchtextbig);
	string searchstring(asciistring);

	//Get the list of search and open each file to do the searching
	for(unsigned int i=0; i<(unsigned int)querylist.size(); i++)
	{
		string filepath = querylist[i];
		filepath.resize(filepath.size()-4);
		filepath += ".txt";
		try
		{
			//Open the file
			CFile cfile;
			cfile.Open(CString(filepath.c_str()), CFile::modeRead);
			char* filecontent = new char[(unsigned int)cfile.GetLength()];
			UINT nbyte = cfile.Read(filecontent, (unsigned int)cfile.GetLength());
			cfile.Close();
			string filestring(filecontent);
			//Find the matching
			if(filestring.find(searchstring)!=filestring.npos)
				queryresult.push_back(querylist[i]);
			//clean memory
			delete[] filecontent;
		}
		catch(CFileException* e)
		{
			e->ReportError();
			return;
		}
	}

	//Display the list
	CListBox* listbox = (CListBox*)CSearchImage::GetDlgItem(IDC_KEYWORDSEARCH_RESULTS);
	for(unsigned int i=0; i<(unsigned int)queryresult.size(); i++)
	{
		string::size_type filename = queryresult[i].find_last_of('\\');
		listbox->AddString(CString(&(queryresult[i].c_str()[++filename])));
	}

	return;
}

void CSearchImage::OnLbnSelchangeKeywordsearchResults()
{
	
}

//Export the result into a folder
void CSearchImage::OnBnClickedKeywordsearchSavetofolder()
{
	//Select a folder
	string folderpath;
	try
	{
		folderpath = bf.SelectAFolder();
	}
	catch(string exceptionmessage)
	{
		::AfxMessageBox(CString(exceptionmessage.c_str()), MB_OK, 0);
		return;
	}

	//Copy the files into the folder
	for(unsigned int i=0; i<(unsigned int)queryresult.size(); i++)
	{
		//Copy the image file fuse the last folder name and filename together to the new image name
		string::size_type filenameposition = queryresult[i].find_last_of('\\');
		string filename(&(queryresult[i].c_str()[filenameposition+1]));
		string newstring(&(queryresult[i].c_str()[filenameposition]), filenameposition-1);
		filenameposition = newstring.find_last_of('\\');
		filename = string(&(newstring.c_str()[filenameposition]))+filename;		//Find the last folder name and add to the image name
		string newfilepath = folderpath+filename;
		std::ifstream oldfile;
		std::ofstream newfile;
		oldfile.open(queryresult[i].c_str(), std::ios::binary);
		newfile.open(newfilepath.c_str(), std::ios::binary);
		newfile << oldfile.rdbuf();
		oldfile.close();
		newfile.close();

		//Copy the caption
		string oldtextfilename(queryresult[i].c_str(), queryresult[i].size()-4);
		oldtextfilename += ".txt";
		newfilepath.resize(newfilepath.size()-4);
		newfilepath += ".txt";
		oldfile.open(oldtextfilename.c_str(), std::ios::binary);
		newfile.open(newfilepath.c_str(), std::ios::binary);
		newfile << oldfile.rdbuf();
		oldfile.close();
		newfile.close();
	}

	::AfxMessageBox(CString("Copy finished"), MB_OK, 0);
	return;
}
